import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Like, Repository } from 'typeorm';
import BaseShutdownReason from '@main/entities/service/admin/base/base-shutdown-reason.entity';
import { ApiException } from '@main/common/exceptions/api.exception';
import { SysOperationLogService } from '../../system/operationLog/operationLog.service';

@Injectable()
export class BaseShutdownReasonService {
  constructor(
    @InjectRepository(BaseShutdownReason)
    private shutdownReasonRepository: Repository<BaseShutdownReason>,
    private operationLogService: SysOperationLogService,
  ) {}

  async list() {
    return await this.shutdownReasonRepository.find();
  }

  /**
   * 罗列所有配置
   */
  async page(data) {
    const { pageSize, pageNum, entity = {} } = data;

    const { shutdownReasonName, shutdownReasonCode } = entity;

    return await this.shutdownReasonRepository.findAndCount({
      where: {
        shutdownReasonName: Like(`%${shutdownReasonName}%`),
        shutdownReasonCode: Like(`%${shutdownReasonCode}%`),
      },
      order: {
        updateTime: 'DESC',
      },
      take: pageSize,
      skip: (pageNum - 1) * pageSize,
    });
  }

  /**
   * 新增
   */
  async add(data, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    const shutdownReason = await this.shutdownReasonRepository.insert({
      ...data,
      createName: user.nickName,
      createUserName: user.userName,
      updateName: user.nickName,
      updateUserName: user.userName,
    });
    databaseOperationLogList.push({
      type: 'add',
      dataType: 'obj',
      database: 'BaseShutdownReason',
      checkOnlyKeyList: [],
      data: shutdownReason,
      whereObj: null,
      remark: `BaseShutdownReason表新增数据`,
    });
    await this.operationLogService.add(
      {
        type: 'add',
        msg: `停机原因管理模块新增停机原因`,
        databaseOperationLogList,
        remark: `新增停机原因${data.shutdownReasonName}`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  /**
   * 更新
   */
  async update(data, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    const { id, shutdownReasonName, remark } = data;

    await this.shutdownReasonRepository.update(
      { id },
      { shutdownReasonName, remark, updateName: user.nickName, updateUserName: user.userName }
    );
    databaseOperationLogList.push({
      type: 'update',
      dataType: 'obj',
      database: 'BaseShutdownReason',
      checkOnlyKeyList: [],
      data: { shutdownReasonName, remark, updateName: user.nickName, updateUserName: user.userName },
      whereObj: { id },
      remark: `BaseShutdownReason表更新停机原因信息`,
    });
    await this.operationLogService.add(
      {
        type: 'update',
        msg: `停机原因管理模块更新停机原因信息`,
        databaseOperationLogList,
        remark: `更新停机原因${data.shutdownReasonName}`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  /**
   * 删除
   */
  async delete(idList: number[], user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    await this.shutdownReasonRepository.delete(idList);
    databaseOperationLogList.push({
      type: 'delete',
      dataType: 'array',
      database: 'BaseShutdownReason',
      checkOnlyKeyList: [],
      data: null,
      whereObj: idList,
      remark: `BaseShutdownReason表删除停机原因`,
    });
    await this.operationLogService.add(
      {
        type: 'delete',
        msg: `停机原因管理模块删除停机原因`,
        databaseOperationLogList,
        remark: `删除停机原因`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  async isExistKey(data) {
    const { shutdownReasonCode } = data;
    const result = await this.shutdownReasonRepository.findOne({ where: { shutdownReasonCode } });

    if (result) throw new ApiException(10048);
  }

  async isExistCode(shutdownReasonCode) {
    const result = await this.shutdownReasonRepository.findOne({ where: { shutdownReasonCode } });

    return result;
  }
}
